package com.chwn.harulib.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.chwn.harulib.R;
import com.gyf.barlibrary.ImmersionBar;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by Myshikin on 2017/3/14.
 */
public abstract class BaseActivity extends AppCompatActivity {

    private   TextView    mTitleTV;
    private   TextView    mBackTV;
    protected View        topView;
    private   ImageButton mBackIB;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutId());
        initData();
        if (isRequredTopBar()) {
            initTopBar();
        }
        initView();
        initListener();

    }

    protected abstract int getLayoutId();

    protected boolean isRequredTopBar() {
        return true;
    }

    private void initTopBar() {
        mTitleTV = (TextView) findViewById(R.id.tv_toolbar_title);
        mBackTV = (TextView) findViewById(R.id.tv_back);
        topView = findViewById(R.id.top_view);
        mBackIB = (ImageButton) findViewById(R.id.ib_back);

        if (mBackTV != null) {
            mBackTV.setVisibility(View.GONE);
        }
    }

    /**
     * 初始化数据--其他页面传来的、页面所需的静态数据
     */
    protected void initData() {

    }

    /**
     * 初始化布局---统一使用ButterKnife
     */
    protected abstract void initView();

    /**
     * 初始化监听
     */
    protected void initListener() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);

    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    protected void setTitleText(String text) {
        mTitleTV.setText(text);
    }

    protected void setBackTVisible(boolean isVisible) {
        mBackTV.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    protected void setBackIcon(int id) {
        mBackIB.setImageResource(id);
    }

    /**
     * 设置底部状态栏颜色Id 传入值为-1时 使用默认颜色
     * @param colorId
     */
    protected void initnavigationBarColor(int colorId) {
        int navigationBarColorId = colorId == -1 ? R.color.colorPrimary : colorId;
        ImmersionBar.with(this)
                .statusBarView(R.id.top_view)
                .statusBarDarkFont(true, 0.2f)
                //                .navigationBarColor(navigationBarColorId)
                .fullScreen(true)
                .addTag("PicAndColor")  //给上面参数打标记，以后可以通过标记恢复
                .init();
    }

 /*   */

    /**
     * 设置沉浸式状态栏
     *//*
    public void setSystemBarTint(int id) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(id);
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }*/
    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        if (!isRequireCustomAnim()) {
            overridePendingTransition(R.anim.next_in, R.anim.next_out);
        }
    }

    protected boolean isRequireCustomAnim() {
        return false;
    }

    @Override
    public void finish() {
        super.finish();
        if (!isRequireCustomAnim()) {
            overridePendingTransition(R.anim.quit_in, R.anim.quit_out);
        }
    }
}
